package com.samxcode.leetcode;

/**
 * Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i,
 * ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0).
 * Find two lines, which together with x-axis forms a container, such that the container contains
 * the most water.
 * 
 * reference:
 * https://leetcode.com/discuss/11482/yet-another-way-to-see-what-happens-in-the-o-n-algorithm
 * 
 * @author Sam
 *
 */
public class ContainerWithMostWater {

    public static void main(String[] args) {
        int[] height = { 1, 1 };
        System.out.println(maxArea(height));
    }


    public static int maxArea(int[] height) {
        int max = 0;
        int i = 0, j = height.length - 1;
        while (i < j) {
            if (height[i] < height[j]) {
                max = max < (j - i) * height[i] ? (j - i) * height[i] : max;
                i++;
            } else {
                max = max < height[j] * (j - i) ? (j - i) * height[j] : max;
                j--;
            }
        }
        return max;
    }
}
